package com.yunke.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunke.common.result.YunkeResult;
import com.yunke.dto.notification.YunkeSendNotificationDto;
import com.yunke.service.YunkeNotificationService;
import com.yunke.vo.notification.YunkeNotificationVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 消息通知控制器
 * 
 * @author yunke
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/notification")
@Tag(name = "消息通知管理", description = "消息通知相关功能接口")
public class YunkeNotificationController {

    @Autowired
    private YunkeNotificationService notificationService;

    @PostMapping("/send")
    @Operation(summary = "发送通知", description = "发送单个通知消息")
    public YunkeResult<Map<String, Object>> sendNotification(
            @Parameter(description = "发送通知信息") @Validated @RequestBody YunkeSendNotificationDto sendDto) {
        try {
            Map<String, Object> result = notificationService.sendNotification(sendDto);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("发送通知失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @PostMapping("/batchSend")
    @Operation(summary = "批量发送通知", description = "批量发送通知消息")
    public YunkeResult<Map<String, Object>> batchSendNotification(
            @Parameter(description = "用户ID列表") @RequestBody List<Long> userIds,
            @Parameter(description = "发送通知信息") @Validated @RequestBody YunkeSendNotificationDto sendDto) {
        try {
            Map<String, Object> result = notificationService.batchSendNotification(userIds, sendDto);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("批量发送通知失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @GetMapping("/my/list")
    @Operation(summary = "获取我的通知列表", description = "分页获取当前用户的通知列表")
    public YunkeResult<Page<YunkeNotificationVo>> getMyNotificationList(
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "页面大小") @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "通知类型") @RequestParam(required = false) Integer notificationType,
            @Parameter(description = "是否已读") @RequestParam(required = false) Integer isRead) {
        try {
            Page<YunkeNotificationVo> result = notificationService.getMyNotificationList(current, size, notificationType, isRead);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("获取我的通知列表失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @GetMapping("/detail/{notificationId}")
    @Operation(summary = "获取通知详情", description = "根据通知ID获取通知详细信息")
    public YunkeResult<YunkeNotificationVo> getNotificationDetail(
            @Parameter(description = "通知ID") @PathVariable Long notificationId) {
        try {
            YunkeNotificationVo result = notificationService.getNotificationDetail(notificationId);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("获取通知详情失败，通知ID：{}", notificationId, e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @PostMapping("/markRead/{notificationId}")
    @Operation(summary = "标记通知为已读", description = "将指定通知标记为已读状态")
    public YunkeResult<Map<String, Object>> markAsRead(
            @Parameter(description = "通知ID") @PathVariable Long notificationId) {
        try {
            Map<String, Object> result = notificationService.markAsRead(notificationId);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("标记通知为已读失败，通知ID：{}", notificationId, e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @PostMapping("/batchMarkRead")
    @Operation(summary = "批量标记为已读", description = "批量标记多个通知为已读状态")
    public YunkeResult<Map<String, Object>> batchMarkAsRead(
            @Parameter(description = "通知ID列表") @RequestBody List<Long> notificationIds) {
        try {
            Map<String, Object> result = notificationService.batchMarkAsRead(notificationIds);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("批量标记为已读失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @PostMapping("/markAllRead")
    @Operation(summary = "全部标记为已读", description = "标记所有或指定类型的通知为已读")
    public YunkeResult<Map<String, Object>> markAllAsRead(
            @Parameter(description = "通知类型（可选）") @RequestParam(required = false) Integer notificationType) {
        try {
            Map<String, Object> result = notificationService.markAllAsRead(notificationType);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("全部标记为已读失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete/{notificationId}")
    @Operation(summary = "删除通知", description = "删除指定的通知")
    public YunkeResult<Map<String, Object>> deleteNotification(
            @Parameter(description = "通知ID") @PathVariable Long notificationId) {
        try {
            Map<String, Object> result = notificationService.deleteNotification(notificationId);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("删除通知失败，通知ID：{}", notificationId, e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/batchDelete")
    @Operation(summary = "批量删除通知", description = "批量删除多个通知")
    public YunkeResult<Map<String, Object>> batchDeleteNotification(
            @Parameter(description = "通知ID列表") @RequestBody List<Long> notificationIds) {
        try {
            Map<String, Object> result = notificationService.batchDeleteNotification(notificationIds);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("批量删除通知失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/clear")
    @Operation(summary = "清空通知", description = "清空所有或指定类型的通知")
    public YunkeResult<Map<String, Object>> clearNotifications(
            @Parameter(description = "通知类型（可选）") @RequestParam(required = false) Integer notificationType) {
        try {
            Map<String, Object> result = notificationService.clearNotifications(notificationType);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("清空通知失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @GetMapping("/unreadCount")
    @Operation(summary = "获取未读消息统计", description = "获取各类型未读消息的数量统计")
    public YunkeResult<Map<String, Object>> getUnreadNotificationCount() {
        try {
            Map<String, Object> result = notificationService.getUnreadNotificationCount();
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("获取未读消息统计失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @GetMapping("/settings")
    @Operation(summary = "获取通知设置", description = "获取当前用户的通知设置")
    public YunkeResult<Map<String, Object>> getNotificationSettings() {
        try {
            Map<String, Object> result = notificationService.getNotificationSettings();
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("获取通知设置失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @PostMapping("/settings/update")
    @Operation(summary = "更新通知设置", description = "更新当前用户的通知设置")
    public YunkeResult<Map<String, Object>> updateNotificationSettings(
            @Parameter(description = "通知设置") @RequestBody Map<String, Object> settings) {
        try {
            Map<String, Object> result = notificationService.updateNotificationSettings(settings);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("更新通知设置失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    // 管理员接口
    @PostMapping("/system/send")
    @Operation(summary = "发送系统通知", description = "管理员发送系统通知")
    public YunkeResult<Map<String, Object>> sendSystemNotification(
            @Parameter(description = "发送通知信息") @Validated @RequestBody YunkeSendNotificationDto sendDto) {
        try {
            Map<String, Object> result = notificationService.sendSystemNotification(sendDto);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("发送系统通知失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @PostMapping("/broadcast")
    @Operation(summary = "发送广播通知", description = "管理员发送广播通知给所有用户")
    public YunkeResult<Map<String, Object>> sendBroadcastNotification(
            @Parameter(description = "发送通知信息") @Validated @RequestBody YunkeSendNotificationDto sendDto) {
        try {
            Map<String, Object> result = notificationService.sendBroadcastNotification(sendDto);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("发送广播通知失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @GetMapping("/admin/statistics")
    @Operation(summary = "获取通知统计", description = "管理员获取通知相关统计数据")
    public YunkeResult<Map<String, Object>> getNotificationStatistics() {
        try {
            Map<String, Object> result = notificationService.getNotificationStatistics();
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("获取通知统计失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }
} 